package com.meitianhui.convenitenbuy.module.mine.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.mine.contract.ReceivingAddressContract;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerAddress;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.ReceivingAddressActivity;
import com.meitianhui.convenitenbuy.util.PreferenceUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amy on 2017/11/13.
 */

public class ReceivingAddressPresenter implements ReceivingAddressContract.Presenter {

    private ReceivingAddressContract.View mView;
    private ReceivingAddressActivity mContext;

    public ReceivingAddressPresenter(ReceivingAddressActivity activity,
                                     ReceivingAddressActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getUserAddressInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());

        MineApiDataSource.getInstance()
                .getUserReceivingAddress(InfoProvider.getAppToken(mContext),
                        "member.consumerAddressFind", new Gson().toJson(map))
                .subscribe(new ResponseObserver<ConsumerAddress>(mContext) {
                    @Override
                    public void onSuccess(ConsumerAddress data) {
                        List<ConsumerAddress.ConsumerData> consumerDatas = data.data;
                        if (consumerDatas.size() > 0) {
                            mView.showAddress(consumerDatas);
                            // 保存到本地
                            PreferenceUtils.saveObject(mContext, Constants.USER_RECEIVING_ADDRESS, consumerDatas);
                        } else {
                            mView.showDataNone();
                        }
                    }
                });
    }

    /**
     * 删除服务器数据
     */
    @Override
    public void deleteReceivingAddress(String address_id) {
        Map<String, String> map = new HashMap<>();
        map.put("address_id", address_id);
        map.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());

        // 因为上传的key值和返回值都是一样的，所以这里就使用其他的
        MineApiDataSource.getInstance()
                .notifyOrAddReceivingAddress(InfoProvider.getAppToken(mContext),
                        "member.consumerAddressRemove", new Gson().toJson(map))
                .subscribe(new ResponseObserver<Object>(App.getInstance()) {
                    @Override
                    public void onSuccess(Object o) {
                        mView.delSucc();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.showError("删除失败");
                    }
                });
    }

    @Override
    public void notifyUserMajorAddress(ConsumerAddress.ConsumerData consumerData) {
        Map<String, String> map = new HashMap<>();
        map.put("address_id", consumerData.getAddress_id());
        map.put("is_major_addr", "Y");
        map.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());

        MineApiDataSource.getInstance()
                .notifyOrAddReceivingAddress(InfoProvider.getAppToken(mContext),
                        "member.consumerAddressEdit", new Gson().toJson(map))
                .subscribe(new ResponseObserver<Object>(App.getInstance()) {
                    @Override
                    public void onSuccess(Object o) {
                        mView.updateDefaultSucc();
                    }
                });
    }

}
